package cn.drenal.capcap.service.impl;

import cn.drenal.capcap.entity.Banner;
import cn.drenal.capcap.mapper.BannerMapper;
import cn.drenal.capcap.service.BannerService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轮播图服务实现类
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerMapper, Banner> implements BannerService {

    @Override
    public Page<Banner> page(Integer current, Integer size, String title, Integer status) {
        Page<Banner> page = new Page<>(current, size);
        LambdaQueryWrapper<Banner> wrapper = new LambdaQueryWrapper<>();
        
        // 根据标题模糊查询
        wrapper.like(StringUtils.isNotBlank(title), Banner::getTitle, title);
        // 根据状态查询
        wrapper.eq(status != null, Banner::getStatus, status);
        // 按排序和创建时间排序
        wrapper.orderByAsc(Banner::getSort);
        wrapper.orderByDesc(Banner::getCreateTime);
        
        return page(page, wrapper);
    }
    
    @Override
    public List<Banner> getEnabledBanners() {
        LambdaQueryWrapper<Banner> wrapper = new LambdaQueryWrapper<>();
        // 只查询启用状态的轮播图
        wrapper.eq(Banner::getStatus, 1);
        // 按排序和创建时间排序
        wrapper.orderByAsc(Banner::getSort);
        wrapper.orderByDesc(Banner::getCreateTime);
        
        return list(wrapper);
    }
}